package com.boarsoft.boar.office.work.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.office.entity.OfficeGroupUser;
import com.boarsoft.boar.office.entity.OfficeWorkInfo;
import com.boarsoft.boar.office.entity.OfficeWorkPre;
import com.boarsoft.boar.office.work.OfficeWorkBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("officeWorkBiz")
public class OfficeWorkBizImpl extends SimpleBizImpl implements OfficeWorkBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<OfficeWorkInfo> list(String groupId, String userId, String key, String orderBy, int pageNo,
			int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(OfficeWorkInfo.class.getName());
		sb.append(" where status != ").append(OfficeWorkInfo.STATUS_CLOSED);
		if (Util.strIsEmpty(groupId)) {
			sb.append(" and ownerId = '").append(userId).append("'");
		} else {
			sb.append(" and ownerId in (select userId from ").append(OfficeGroupUser.class.getName());
			sb.append(" where groupId = '").append(groupId).append("')");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (title like '%").append(key);
			sb.append("%') ");
		}
		//
		int total = dao.getTotal("select count(1)".concat(sb.toString()));
		if (Util.strIsEmpty(orderBy)) {
			sb.append(" order by ownerId, planEndDate, planStartDate desc");
		} else {
			sb.append(" order by ").append(orderBy);
		}
		//
		List<OfficeWorkInfo> lt = dao.list(sb.toString(), OfficeWorkInfo.class, pageNo, pageSize);
		return new PagedResult<OfficeWorkInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public OfficeWorkInfo get(String id) {
		OfficeWorkInfo o = dao.get(OfficeWorkInfo.class, id);
		o.getPreWorks().size();
		return o;
	}

	@Override
	@Transactional
	public void save(OfficeWorkInfo o) {
		this.save(o, null);
	}

	@Override
	@Transactional
	public void save(OfficeWorkInfo o, String preWorkIds) {
		//
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
		//
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(OfficeWorkPre.class.getName());
		sb.append(" where workId='").append(o.getId()).append("'");
		dao.executeUpdate(sb.toString());
		//
		if (Util.strIsEmpty(preWorkIds)) {
			return;
		}
		String[] pa = preWorkIds.split(",");
		for (String p : pa) {
			dao.save(new OfficeWorkPre(o.getId(), p));
		}
	}

	@Override
	@Transactional
	public void update(OfficeWorkInfo o) {
		dao.merge(o);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<OfficeWorkInfo> list(String groupId, String ownerId, String key, Short status, String orderBy,
			int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(OfficeWorkInfo.class.getName());
		sb.append(" where 1=1");
		if (status != null && status > 0) {
			sb.append(" and status = ").append(status);
		}
		if (Util.strIsEmpty(ownerId)) {
			if (Util.strIsEmpty(groupId)) {
				// TODO
			} else {
				sb.append(" and ownerId in (select userId from ").append(OfficeGroupUser.class.getName());
				sb.append(" where groupId = '").append(groupId).append("')");
			}
		} else {
			sb.append(" and ownerId = '").append(ownerId).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (title like '%").append(key);
			sb.append("%') ");
		}
		//
		int total = dao.getTotal("select count(1)".concat(sb.toString()));
		if (Util.strIsEmpty(orderBy)) {
			sb.append(" order by ownerId, planEndDate, planStartDate desc");
		} else {
			sb.append(" order by ").append(orderBy);
		}
		//
		List<OfficeWorkInfo> lt = dao.list(sb.toString(), OfficeWorkInfo.class, pageNo, pageSize);
		return new PagedResult<OfficeWorkInfo>(total, lt, pageNo, pageSize);
	}
}